package com.jrx.anytxn.investor.service.impl;

import com.jrx.anytxn.investor.service.IInvestorStrategySelector;
import com.jrx.anytxn.investor.service.IInvestorTransferMethodStrategy;
import com.jrx.anytxn.investor.service.IInvestorTransferTypeStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * 转列策略选择器
 *
 * @author liuyang
 * @date 2020-05-27
 **/
@Service
public class InvestorTransferStrategySelector implements IInvestorStrategySelector {

    @Autowired
    private List<IInvestorTransferMethodStrategy> transferMethodStrategyList;
    @Autowired
    private List<IInvestorTransferTypeStrategy> transferTypeServiceList;

    /**
     * 获取转列方法转列策略
     *
     * @param tansferMethod 转列方式
     * @return IInvestorTransferMethodStrategy 转列策略
     * @author liuyang
     * @date 2020-05-27
     */
    @Override
    public IInvestorTransferMethodStrategy selectMethodStrategy(String tansferMethod){
        if(Integer.parseInt(tansferMethod)+1>transferMethodStrategyList.size()){
            throw new RuntimeException("配置中的转列法"+tansferMethod+"未实现ITransferMethodStrategy接口");
        }

        return  transferMethodStrategyList.get(Integer.parseInt(tansferMethod));
    }

    /**
     * 获取账户类型转列策略
     *
     * @param o
     * @return IInvestorTransferTypeStrategy 转列策略
     * @author liuyang
     * @date 2020-05-27
     */
    @Override
    public IInvestorTransferTypeStrategy selectTypeStrategy(Object o){
        for(IInvestorTransferTypeStrategy transferService : transferTypeServiceList){
            ParameterizedType type = (ParameterizedType)transferService.getClass().getGenericInterfaces()[0];
            String className = type.getActualTypeArguments()[0].getTypeName();
           if(className.equals(o.getClass().getName())) {
               return transferService;
           }

        }
        throw new RuntimeException("未实现ITransferTypeStrategy<"+o.getClass().getName()+">接口");
    }
}
